﻿using System;
using System.IO;
using System.Web;
using System.Xml;
using HNAlbum.Logic.Entity;

namespace HNAlbum.Logic.Manager
{
    public class UserManager : ManagerBase
    {
        public HNAlbumUser GetUser(string login)
        {
            HNAlbumUser user = null;

            //Try load the user xml data file
            XmlDocument userXML = new XmlDocument();
            try
            {
                //Load XML
                userXML.Load(HttpContext.Current.Server.MapPath(Path.Combine(ApplicationFoler.XML, ApplicationXMLFile.User)));
                //Get user nodes
                XmlNodeList userNodes = userXML.SelectNodes("//User");
                //Loop node list
                foreach (XmlNode node in userNodes)
                {
                    if (node.ChildNodes[0].InnerText.Equals(login))
                    {
                        user = new HNAlbumUser();
                        user.Login = node.ChildNodes[0].InnerText;
                        user.Password = node.ChildNodes[1].InnerText;
                        user.Name = node.ChildNodes[2].InnerText;
                        user.Email = node.ChildNodes[3].InnerText;
                        user.Role = (HNAlbumUser.SecurityRole)Convert.ToInt32(node.ChildNodes[4].InnerText);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                LogAndThrow(ex, HNAException.User.CannotRead);
            }

            return user;
        }
    }
}
